import Vue from 'vue';
import Router from 'vue-router';
import service from '../utils/request';

// 修复路由重复导航报错问题
const originalReplace = Router.prototype.replace;
Router.prototype.replace = function replace(location) {
    return originalReplace.call(this, location).catch(err => err);
};
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err);
};

Vue.use(Router);

export default new Router({
    routes: [

        {
            path: '/auth-redirect',
            component: () => import('../components/page/auth-redirect.vue'),
            meta: { 
                title: '登录跳转',
                requiresAuth: false  // ✅ 明确标记为公开路由
            },
            hidden: true
        },
        {
            path: '/',
            redirect: '/index',
            meta: { requiresAuth: false }
        },
        {
            path: '/index',
            component: () => import('../components/page/index.vue'),
            meta: { 
                title: '二手物品交易平台',
                requiresAuth: false  // ✅ 首页不需要认证
            }
        },
        {
            path: '/callback',
            component: () => import('../components/page/index.vue'),
            meta: { requiresAuth: false }  // ✅ 回调页面不需要认证
        },
        {
            path: '/search',
            component: () => import('../components/page/search.vue'),
            meta: { 
                title: '闲置二手物品 | 二手物品交易平台',
                requiresAuth: false  // ✅ 搜索页不需要认证
            }
        },
        {
            path: '/me',
            component: () => import('../components/page/me.vue'),
            meta: { 
                title: '个人中心 | 二手物品交易平台',
                requiresAuth: true  // ✅ 需要登录才能访问
            }
        },
        {
            path: '/message',
            component: () => import('../components/page/message.vue'),
            meta: { 
                title: '消息 | 二手物品交易平台',
                requiresAuth: true  // ✅ 需要登录
            }
        },
        {
            path: '/release',
            component: () => import('../components/page/release.vue'),
            meta: { 
                title: '发布二手物品 | 二手物品交易平台',
                requiresAuth: true  // ✅ 需要登录
            }
        },
        {
            path: '/details',
            component: () => import('../components/page/idle-details.vue'),
            meta: { 
                title: '二手物品详情 | 二手物品交易平台',
                requiresAuth: false  // ✅ 详情页公开
            }
        },
        {
            path: '/order',
            component: () => import('../components/page/order.vue'),
            meta: { 
                title: '订单详情 | 二手物品交易平台',
                requiresAuth: true  // ✅ 需要登录
            }
        },
        {
            path: '/login',
            component: () => import('../components/page/login.vue'),
            meta: { 
                title: '登录 | 二手物品交易平台',
                requiresAuth: false  // ✅ 登录页不需要认证
            }
        },
        {
            path: '/sign-in',
            component: () => import('../components/page/sign-in.vue'),
            meta: { 
                title: '注册 | 二手物品交易平台',
                requiresAuth: false  // ✅ 注册页不需要认证
            }
        },
        {
            path: '/login-admin',
            component: () => import('../components/page/login-admin.vue'),
            meta: { 
                title: '后台登陆',
                requiresAuth: true  // ✅ 需要管理员权限
            }
        },
        {
            path: '/platform-admin',
            component: () => import('../components/page/platform-admin.vue'),
            meta: { 
                title: '后台管理',
                requiresAuth: true  // ✅ 需要管理员权限
            }
        },
        {
            path: '*',
            redirect: '/',
            meta: { requiresAuth: false }
        }
    ]
});